package com.sgxy.wmservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgxy.servicebase.exceptionhandler.ERPException;
import com.sgxy.wmservice.entity.WmGoods;
import com.sgxy.wmservice.entity.WmOrders;
import com.sgxy.wmservice.entity.WmOutputOrder;
import com.sgxy.wmservice.entity.WmStoreGoods;
import com.sgxy.wmservice.entity.vo.GoodsVo;
import com.sgxy.wmservice.mapper.WmGoodsMapper;
import com.sgxy.wmservice.service.WmGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgxy.wmservice.service.WmOrdersService;
import com.sgxy.wmservice.service.WmOutputOrderService;
import com.sgxy.wmservice.service.WmStoreGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@Service
public class WmGoodsServiceImpl extends ServiceImpl<WmGoodsMapper, WmGoods> implements WmGoodsService {

    @Autowired
    private WmOrdersService wmOrdersService;

    @Autowired
    private WmOutputOrderService wmOutputOrderService;

    @Autowired
    private WmStoreGoodsService wmStoreGoodsService;

    @Override
    public IPage<GoodsVo> selectPageVoList(IPage<GoodsVo> page, WmGoods wmGoods) {
        return baseMapper.selectPageVoList(page,wmGoods);
    }

    @Override
    public GoodsVo selectOneById(String id) {
        return baseMapper.selectOneById(id);
    }

    @Override
    public Boolean deleteGoodsById(String id) {

        QueryWrapper<WmOrders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("goods_id",id);
        int count1 = wmOrdersService.count(ordersQueryWrapper);
        if(count1>0){
            throw new ERPException(20001,"该商品正被使用，无法删除");
        }

        QueryWrapper<WmOutputOrder> outputOrderQueryWrapper = new QueryWrapper<>();
        outputOrderQueryWrapper.eq("goods_id",id);
        int count2 = wmOutputOrderService.count(outputOrderQueryWrapper);
        if(count2>0){
            throw new ERPException(20001,"该商品正被使用，无法删除");
        }

        QueryWrapper<WmStoreGoods> storeGoodsQueryWrapper = new QueryWrapper<>();
        storeGoodsQueryWrapper.eq("goods_id",id);
        int count3 = wmStoreGoodsService.count(storeGoodsQueryWrapper);
        if(count3>0){
            throw new ERPException(20001,"该商品正被使用，无法删除");
        }

        baseMapper.deleteById(id);
        return true;
    }
}
